<!--
 * @Author: early-autumn
 * @Date: 2020-05-18 14:42:40
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-05-18 15:23:53
-->
<template>
  <el-dialog :visible="value" :title="title" width="1200" @close="handleClose">
    <el-card class="box-card">
      <el-page-header v-if="userPerson.name" class="pageHeader">
        <div slot="content" class="content">
          <el-form inline>
            <el-form-item :label="userPerson.name"></el-form-item>
            <el-form-item :label="`${userPerson.age}`"></el-form-item>
            <el-form-item :label="sexEnum[userPerson.sex]"></el-form-item>
            <el-form-item label="手机号：">
              {{ userPerson.phoneNumber }}
            </el-form-item>
            <el-form-item label="病历：">
              {{ userPerson.caseNumber }}
            </el-form-item>
            <el-form-item label="TC：">
              {{ userAppointment.tcName }}
            </el-form-item>
            <el-form-item label="医生：">
              {{ userAppointment.doctorName }}
            </el-form-item>
          </el-form>
        </div>
      </el-page-header>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="门诊患者id" prop="clinicPersonId">
          <el-input v-model="form.clinicPersonId" placeholder="请输入门诊患者id" />
        </el-form-item>-->
        <!-- <el-form-item label="资源地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入资源地址" />
        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="就诊时间:"
              prop="clinicTime"
              label-width="90px"
            >
              <el-date-picker
                v-model="form.clinicTime"
                type="datetime"
                placeholder="选择预约时间"
                size="small"
                style="width: 200px"
                align="right"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源类型" prop="type">
              <el-select
                v-model="form.type"
                clearable
                placeholder="请选择资源类型"
              >
                <el-option
                  v-for="item in resourceShadow"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
        <el-form-item label="上传图片" prop="url" class="upload">
          <el-upload
            action="#"
            :limit="8"
            :file-list="imgFileList"
            list-type="picture-card"
            :before-upload="beforeUpload"
            :http-request="requestUpload"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :append-to-body="true" :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
      </el-form>
    </el-card>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import oss from '@/mixins/oss'
import { sexEnum } from '@/utils/enums'

export default {
  name: 'Upload',
  mixins: [oss],
  model: {
    prop: 'value',
    event: 'close',
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '',
    },
    userPerson: {
      type: Object,
      default: () => ({}),
    },
    userAppointment: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      form: {
        url: [],
      },
      imgFileList: [],
      hideUpload: false, //是否显示上传图片的加号
      dialogImageUrl: '',
      dialogVisible: false,
      rules: {
        clinicTime: [
          { required: true, message: '就诊时间不能为空', trigger: 'blur' },
        ],
        url: [{ required: true, message: '资源地址不能为空', trigger: 'blur' }],
        type: [
          { required: true, message: '资源类型不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  computed: {
    ...mapGetters('dict', ['resourceShadow']),
    sexEnum() {
      return sexEnum
    },
  },
  watch: {
    value(val) {
      if (val) {
        this.hideUpload = false
      }
    },
  },
  methods: {
    handleClose() {
      this.resetForm()
      this.$emit('change', false)
    },
    handleRemove(file, fileList) {
      this.imgFileList = fileList
      this.hideUpload = false
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    requestUpload(file) {
      this.cropperUpload('resourceShadow', file.file, this.ossData).then(
        (response) => {
          if (response.res.status === 200 && response.res.statusCode === 200) {
            const imageUrl = `https://${this.host}/${response.name}`
            const obj = {
              name: file.file.name,
              uid: file.file.uid,
              url: imageUrl,
            }
            this.imgFileList.push(obj)
            if (!this.form.url) {
              this.form.url = []
            }

            this.form.url.push(obj)
            this.hideUpload = true
          } else {
            this.msgError(response.msg)
          }
        }
      )
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.imgFileList.length == 0) {
            this.$message.error('亲,您还没有上传图片!')
            return
          } else {
            this.form.url = this.imgFileList
            this.form.clinicTime = this.parseTime(
              this.form.clinicTime,
              '{y}-{m}-{d} {h}:{i}:{s}'
            )
            this.$emit('submit', { ...this.form })
            this.resetForm()
          }
        }
      })
    },
    resetForm() {
      this.$refs['form'].resetFields()
      this.imgFileList = []
      this.form = {}
      this.$emit('change', false)
    },
  },
}
</script>

<style lang="scss" scoped>
.hide >>> .el-upload--picture-card {
  display: none;
}
.filingBox {
  display: flex;
}
.filingBox:nth-child(1) div {
  margin-right: 10px;
}
.pageHeader >>> .el-page-header__left {
  display: none;
}
</style>
